home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-12-12 | 4.4 KB | 223 lines | [TEXT/CWIE] |
- /***
- * GOSAScript.cp
- *
- * Methods to implement a script access.
- *
- * Gordon Watts (gwatts@fnal.fnal.gov) © 1995 As Is!
- ***/
-
- #include "GOSAScriptComponent.h"
- #include "GOSAScript.h"
- #include "GOSAID.h"
- #include "StAERecord.h"
- #include "debug.h"
-
- /**
- * GOSAScript
- *
- * Buidl the object up.
- *
- **/
- GOSAScript :: GOSAScript (const GOSAScriptComponent *theComponent)
- : GOSAID (theComponent)
- {
- mScriptContext = new GOSAID (mScriptingComponent, kOSANullScript);
- }
- GOSAScript :: GOSAScript (void)
- {
- mScriptContext = new GOSAID (mScriptingComponent, kOSANullScript);
- }
-
- GOSAScript :: GOSAScript (const GOSAScriptComponent *theComponent,
- const OSAID theId)
- : GOSAID (theComponent, theId)
- {
- mScriptContext = new GOSAID (mScriptingComponent, kOSANullScript);
- }
- GOSAScript :: GOSAScript (const OSAID theId)
- : GOSAID (theId)
- {
- mScriptContext = new GOSAID (mScriptingComponent, kOSANullScript);
- }
- GOSAScript :: GOSAScript (const GOSAScriptComponent *theComponent,
- const FSSpec &theFile)
- : GOSAID (theComponent)
- {
- mScriptContext = new GOSAID (theComponent, kOSANullScript);
- LoadScript (theFile);
- }
- GOSAScript :: GOSAScript (const FSSpec &theFile)
- {
- mScriptContext = new GOSAID (mScriptingComponent, kOSANullScript);
- LoadScript (theFile);
- }
-
- /**
- * ~GOSAScript
- *
- * Delete everything!
- *
- **/
- GOSAScript :: ~GOSAScript (void)
- {
- if (mScriptContext)
- delete mScriptContext;
- }
-
- /**
- * LoadScript
- *
- * Load a compiled script from the given fsspec. Bomb if we can't seem to get
- * one in!
- *
- **/
- void GOSAScript :: LoadScript (const FSSpec &theSpec)
- {
- /**
- ** Get the resource handle for the script.
- **/
-
- short fileRef;
-
- fileRef = FSpOpenResFile (&theSpec, fsRdPerm);
- ThrowIfOSErr_(ResError());
-
- Handle scriptHandle;
-
- scriptHandle = Get1Resource (kOSAScriptResourceType, 128);
- ThrowIfNil_(scriptHandle);
-
- /**
- ** Build a AEDesc out of it, and then get it loaded.
- **/
-
- Try_ {
- AEDesc scriptDesc;
-
- scriptDesc.descriptorType = typeOSAGenericStorage;
- scriptDesc.dataHandle = scriptHandle;
-
- /**
- ** Get a second routine to do the work!
- **/
-
- LoadScript (scriptDesc);
-
- } Catch_(err) {
- if (scriptHandle)
- ReleaseResource (scriptHandle);
- CloseResFile (fileRef);
- Throw_(err);
- }
-
- ReleaseResource (scriptHandle);
- CloseResFile (fileRef);
- }
-
- /**
- * LoadScript
- *
- * The contents of the script are in a AEDesc -- load the damm thing.
- * Save the id so we can execute the script later. Mark the id as valid
- * if the load went ok.
- *
- **/
- void GOSAScript :: LoadScript (const AEDesc &theScript)
- {
- OSAError err;
-
- err = OSALoad (mScriptingComponent->instance(),
- &theScript, kOSAModeNull, &mID);
- mIDValid = err == noErr;
- }
-
- /**
- * Execute
- *
- * Run the script -- shove the returned id into an GOSAID.
- *
- **/
- void GOSAScript :: Execute (GOSAID &theResult)
- {
- AssertStr (mIDValid, "\pAttempt to execute script with invalid id!");
-
- /**
- ** Make sure we have something to execute.
- **/
-
- theResult.SetID (kOSANullScript);
-
- if (mID != kOSANullScript) {
- OSAError err;
- OSAID theResultID;
-
- err = OSAExecute (mScriptingComponent->instance(), mID,
- //mScriptContext->id(),
- kOSANullScript, kOSAModeNull, &theResultID);
- ThrowIfOSErr_(err);
-
- if (theResultID != kOSANullScript)
- theResult.SetID (theResultID);
- }
- }
-
- void GOSAScript :: Execute (AEDesc &theResult)
- {
- GOSAID theResultObj (mScriptingComponent);
-
- Execute (theResultObj);
-
- theResultObj.GetAEDesc (theResult);
- }
-
- void GOSAScript :: Execute (void)
- {
- StAEDescriptor temp;
-
- Execute (temp.mDesc);
- }
-
- /**
- * DoEvent
- *
- * Execute an event in this context.
- *
- **/
- void GOSAScript :: DoEvent (const AppleEvent &theEvent, AppleEvent &theReply) const
- {
- OSAError err;
-
- AssertStr (mIDValid, "\pAttempt to execute invalid id script");
-
- err = OSADoEvent (mScriptingComponent->instance (),
- &theEvent, mID, kOSAModeNull, &theReply);
-
- ThrowIfOSErr_ (err);
- }
-
- /**
- * LoadFromResource
- *
- * We have a script resource -- load in the damm script!
- *
- **/
- void GOSAScript :: LoadFromResource (const StringPtr resourceName, const OSType resType)
- {
- StResource scriptResource (resType, resourceName, true, true);
-
- LoadFromHandle (scriptResource);
- }
-
- /**
- * LoadFromHandle
- *
- * Given a handle containing the script data (like a resource handle),
- * remember it!
- **/
- void GOSAScript :: LoadFromHandle (const Handle theScriptHandle)
- {
- const AEDesc scriptData = {typeOSAGenericStorage, theScriptHandle};
-
- LoadScript (scriptData);
- }
-